package com.handong.handserialprogram.module.setting;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Printer;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.SPUtil;

import java.security.PrivateKey;

public class GPSSettingActivity extends FragmentActivity implements View.OnClickListener {
    private RadioButton mes1, mes2;
    private TextView headerCenterTitle, mBtnStarDownload;
    private ImageView headerImage;
    private EditText mAPN1, mAPN2, mAPN3;
    private String apn1, apn2, apn3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_p_s_setting);
        initUI();
        initData();

    }

    private void initUI() {
        mes1 = findViewById(R.id.radioButton_mes1);
        mes2 = findViewById(R.id.radioButton_mes2);

        mBtnStarDownload = findViewById(R.id.headerOptionItem);
        mBtnStarDownload.setOnClickListener(this);
        headerCenterTitle = findViewById(R.id.headerCenterTitle);

//        mAPN1 = findViewById(R.id.apn1);
//        mAPN2 = findViewById(R.id.apn2);
//        mAPN3 = findViewById(R.id.apn3);

        headerImage = findViewById(R.id.headerBackLayoutIcon);
        headerImage.setImageResource(R.drawable.ic_menu_back_144);
        findViewById(R.id.headerBackLayout).setOnClickListener(this);


    }

    private void initData() {
        headerCenterTitle.setText("定位模式");
        mBtnStarDownload.setVisibility(View.VISIBLE);
        findViewById(R.id.headerOptionLayout).setVisibility(View.VISIBLE);
        mBtnStarDownload.setText("确定");

        chooseRadioGroup();
    }

    private void chooseRadioGroup() {
        if (SPUtil.getBooleanValue(SPUtil.OPEN_GD_GPS)) {
            mes1.setChecked(true);
        } else {
            mes2.setChecked(true);
        }
//        String st1 = SPUtil.getStringValue(SPUtil.FOOT_RING_APN_1);
//        String st2 = SPUtil.getStringValue(SPUtil.FOOT_RING_APN_2);
//        String st3 = SPUtil.getStringValue(SPUtil.FOOT_RING_APN_3);
//        mAPN1.setText(st1);
//        mAPN2.setText(st2);
//        mAPN3.setText(st3);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.headerOptionItem:
                if (saveAndPush()) {
                    pushMes();
                    Intent intent = new Intent(this, SettingActivity.class);
                    startActivity(intent);
                    finish();
                }

                break;
            case R.id.headerBackLayout:
                onBackPressed();
                break;
        }
    }

    private boolean saveAndPush() {
//        apn1 = mAPN1.getText().toString().trim();
//        apn2 = mAPN2.getText().toString().trim();
//        apn3 = mAPN3.getText().toString().trim();
//        if (apn1.length() == 0) {
//            AppUtil.showShortToast(this, "请检查APN1配置");
//            return false;
//        }
//        if (apn3.length() != 0 && apn2.length() == 0) {
//            AppUtil.showShortToast(this, "请检查用户名和密码");
//            return false;
//        }
        return true;
    }

    private void pushMes() {
        SPUtil.putBooleanValue(SPUtil.OPEN_GD_GPS, mes1.isChecked());
//        SPUtil.putStringValue(SPUtil.FOOT_RING_APN_1, apn1);
//        SPUtil.putStringValue(SPUtil.FOOT_RING_APN_2, apn2);
//        SPUtil.putStringValue(SPUtil.FOOT_RING_APN_3, apn3);

    }


}